﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.IO;
using ExamAnalysis.Core;
using ExamAnalysis.Model;

namespace ExamAnalysis.Account
{
    public partial class AccountBind : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                var Student = from s in db.StudentInfo
                              select new { ID = "学生" + s.StudentID.ToString(), Code = s.StudentCode, Name = s.StudentName, Type = "学生", Account = s == null ? "无" : s.AccountID.ToString() };
                var Teacher = from t in db.TeacherInfo
                              select new { ID = "教师" + t.TeacherID.ToString(), Code = t.TeacherCode, Name = t.TeacherName, Type = "教师", Account = t == null ? "无" : t.AccountID.ToString() };
                var User = Student.Concat(Teacher);
                Store2.DataSource = User;
                Store2.DataBind();
            }
        }

        protected void Button2_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            string userid = null;
            RowSelectionModel sm = this.GridPanel2.GetSelectionModel() as RowSelectionModel;
            if (sm.SelectedRows.Count != 0 && ComboBox1.SelectedItem!=null)
            {
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    foreach (SelectedRow row in sm.SelectedRows)
                    {
                        userid = row.RecordID.ToString();
                    }
                    if (userid.Substring(0, 2) == "学生")
                    {
                        var Student = from s in db.StudentInfo
                                      where s.StudentID == Convert.ToInt32(userid.Substring(2, userid.Length - 2))
                                      select s;
                        ExamAnalysis.Model.StudentInfo newstu = Student.First();
                        newstu.AccountID = ComboBox1.SelectedItem.Value;
                        db.SubmitChanges();
                        X.Msg.Alert("提示信息", String.Format("成功修改信息")).Show();
                    }
                    else
                    {
                        var Teacher = from t in db.TeacherInfo
                                      where t.TeacherID == Convert.ToInt32(userid.Substring(4, userid.Length - 4))
                                      select t;
                        ExamAnalysis.Model.TeacherInfo newtea = Teacher.First();
                        db.SubmitChanges();
                        X.Msg.Alert("提示信息", String.Format("成功修改信息")).Show();
                    }
                }   
            }
            else
            {
                X.Msg.Alert("提示信息", "请选择一个成员").Show();
            }
        }
    }
}